package inet.app.vod.activity.film;

import inet.app.vod.activity.R;
import inet.app.vod.activity.common.GestureActivity;
import inet.app.vod.config.AppConstants;
import inet.app.vod.listeners.LockScreenListener;
import inet.app.vod.net.http.VolleyClient;
import inet.app.vod.net.http.VolleyListener;
import inet.app.vod.object.MovieModel;
import inet.app.vod.object.SubtitleSettingModel;
import inet.app.vod.parser.Parse;
import inet.app.vod.subtitles.SubtitleController;
import inet.app.vod.subtitles.SubtitleController.SubtitleListener;
import inet.app.vod.util.BrightnessUtil;
import inet.app.vod.util.MyPreferenceManager;
import inet.app.vod.video.MyMediaController;
import inet.app.vod.video.MyMediaController.OnShowListener;
import inet.app.vod.video.MyMediaControllerBase;
import inet.app.vod.video.MyVideoView;
import inet.app.vod.view.LockableScrollView;
import io.vov.vitamio.LibsChecker;
import io.vov.vitamio.MediaPlayer;

import java.io.File;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.text.Html;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.inet.app.framework.device.DeviceInfoModel;
import com.inet.app.framework.device.DeviceUtil;
import com.inet.app.framework.intentutil.LaunchIntent;
import com.inet.app.framework.util.Log;
import com.inet.app.framework.util.hardware.ConvertUtil;
import com.inet.app.framework.util.text.StringUtil;
import com.inet.app.framework.widget.MyToast;

public class VideoPlayerActivity extends GestureActivity implements
		OnClickListener, LockScreenListener, SubtitleListener {

	enum TYPE_INTENT {
		FROM_APP, OUT_APP_AND_PLAY, OUT_APP_AND_NO_PLAY
	}

	private final static int MENU_SETTING = 0;
	private final static String LINK_TEST = "http://www.cybertechmedia.com/samples/oldworldindustries.wmv";

	private static final String pathSubtiles = Environment
			.getExternalStorageDirectory().getPath()
			+ File.separator
			+ "VOD"
			+ File.separator + "subtiles" + File.separator + "english_en.srt";

	private RelativeLayout mVideoLayout;
	private MyVideoView mVideoView;
	private ProgressBar mProgressBar;
	private MyMediaController mController;
	private MyMediaControllerBase mControllerBase;
	private TextView mSubtitleText;
	private ImageButton mSettingBtn;
	private LockableScrollView mScrollView;

	private MovieModel mMovieSelected;
	private DeviceInfoModel mDevice;
	private int mDisplayWidth;
	private int mDisplayHeight;
	private float mRatioDevice;
	private long mVideoTimeCurrent;
	private int mAppBrightness;
	private int mSystemBrightness;
	private boolean isOpenVideoFromApp;
	private Uri mFileUri;
	private boolean isControllerButton;
	private SubtitleSettingModel mSubtitleModel;
	private SubtitleController mSubtitleController;
	private boolean isSubLoaded = false;
	private String mHomeId;
	private MyPreferenceManager mPref;
	private String mToken;
	private String mUserName;
	private String mLinkWithToken;
	private int mEpCurrent;
	private int mOrientation;
	private boolean mControllerIsLocked;

	private RelativeLayout.LayoutParams mFullParams;
	private RelativeLayout.LayoutParams mWrapParams;

	private View.OnClickListener shareListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			shareItem("iPhim", mMovieSelected.getFbLink(),
					mMovieSelected.getTitle(), mMovieSelected.getImg());
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mDevice = DeviceUtil.getSimpleDeviceInfo(this);
		mDisplayWidth = mDevice.getWidth();
		mDisplayHeight = mDevice.getHeight();
		if (mDevice.getOrientation() == Configuration.ORIENTATION_LANDSCAPE) {
			// full screen at landscape
			getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
					WindowManager.LayoutParams.FLAG_FULLSCREEN);
			getWindow().setFormat(PixelFormat.TRANSLUCENT);
		}

		if (!initVitamio()) {
			Log.e("init vitamio failed!");
			this.finish();
		}

		setContentView(R.layout.activity_video_player);
		initUIBase();

		// control brightness for this screen
		mApplication.mWindowBrightness = getWindow();

		Intent intent = getIntent();
		switch (checkIntentFromApp(intent)) {
		case FROM_APP:
			// stream video from own server
			isOpenVideoFromApp = true;
			openVideoFromApp(savedInstanceState);
			break;
		case OUT_APP_AND_PLAY:
			// open video from file sdcard
			isOpenVideoFromApp = false;
			openVideoFromDevice(savedInstanceState, intent);
			break;
		case OUT_APP_AND_NO_PLAY:
			// no play
			this.finish();
			break;
		default:
			break;
		}
	}

	/**
	 * stream video from own server
	 */
	private void openVideoFromApp(Bundle savedInstanceState) {
		initModels();
		initViews();
		if (savedInstanceState != null) {
			/* continue play video after change orientation */
			mVideoTimeCurrent = savedInstanceState.getLong("video_time");
			if (mVideoTimeCurrent > 0) {
				startVideo(mVideoTimeCurrent);
			}
		}
	}

	/**
	 * Open file video from sdcard
	 */
	private void openVideoFromDevice(Bundle savedInstanceState, Intent intent) {
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		mFileUri = intent.getData();
		if (mFileUri == null) {
			MyToast.show(this, "Không thể mở file vừa chọn");
			this.finish();
		} else {
			initModels();
			initViews();
		}
	}

	/**
	 * Check intent call video player from in-app or out-app
	 */
	private TYPE_INTENT checkIntentFromApp(Intent intent) {

		String action = intent.getAction();
		String type = intent.getType();
		if (null == action) {
			return TYPE_INTENT.FROM_APP;
		}
		if (Intent.ACTION_VIEW.equals(action) && type != null) {
			if (type.startsWith("video/")) {
				return TYPE_INTENT.OUT_APP_AND_PLAY;
			}
		}
		return TYPE_INTENT.OUT_APP_AND_NO_PLAY;
	}

	/**
	 * Check type media controller button or controller gesture
	 */
	private boolean isControllerButton() {
		return (MyPreferenceManager.getInstance(mContext).getTypeController() == MyMediaControllerBase.TYPE_BUTTON);
	}

	private void initModels() {

		mPref = MyPreferenceManager.getInstance(getApplicationContext());

		if (isOpenVideoFromApp) {
			Bundle bundle = getIntent().getExtras();
			if (bundle != null) {
				mMovieSelected = (MovieModel) bundle.get("movie");
				mHomeId = bundle.getString("home_id");
				mEpCurrent = bundle.getInt("episode");
			}
			if (mMovieSelected == null) {
				MyToast.show(this, "Không tìm thấy phim này");
				finish();
				return;
			}
		}

		mRatioDevice = ((float) mDevice.getWidth()) / mDevice.getHeight();

		mSystemBrightness = BrightnessUtil
				.getSystemBrightness(getContentResolver());

		mAppBrightness = MyPreferenceManager.getInstance(mContext)
				.getBrightness();

		if (mAppBrightness < 0) {
			// no setting, set value brightness = system brightness
			Log.d("no setting brightness, use system brightness");
			mAppBrightness = mSystemBrightness;
		}
		mFullParams = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.MATCH_PARENT,
				RelativeLayout.LayoutParams.MATCH_PARENT);

		mWrapParams = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.MATCH_PARENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		mWrapParams.addRule(RelativeLayout.BELOW, R.id.title_layout);
	}

	private void initViews() {
		mScrollView = (LockableScrollView) findViewById(R.id.scroll_view);
		findViewById(R.id.share_button).setOnClickListener(shareListener);

		((TextView) findViewById(R.id.title_text)).setText(mMovieSelected
				.getTitle());

		mVideoLayout = (RelativeLayout) findViewById(R.id.video_layout);

		mVideoView = (MyVideoView) findViewById(R.id.video_view);
		mSubtitleText = (TextView) findViewById(R.id.subtitle_text);

		// Attach subtitleController to videoview
		mSubtitleController = new SubtitleController(mVideoView);
		mSubtitleController.setSubtitleListener(this);

		if (mDevice.getOrientation() == Configuration.ORIENTATION_LANDSCAPE) {
			int typeScale = MyVideoView.VIDEO_LAYOUT_STRETCH;
			mVideoView.setVideoLayout(typeScale, mRatioDevice);
		}

		mSettingBtn = (ImageButton) findViewById(R.id.setting_btn);
		mSettingBtn.setOnClickListener(mSettingListener);

		setTitle(getResources().getString(R.string.xem_phim));

		isControllerButton = isControllerButton();
		initController();
		setBrightness(mAppBrightness);

		// TODO test
		// mToken = mPref.getRootToken();
		// if (TextUtils.isEmpty(mToken)) {
		// showErrorGetVideoLink();
		// }
		// String rawLink = mMovieSelected.getLinkMovie(mEpCurrent);
		// mLinkWithToken = rawLink + "?token=" + mToken;
		// initVideoView();

		requestToken();

		requestSubtitles();
	}

	private void initController() {

		initGestureView();

		if (isControllerButton) {
			mController = new MyMediaController(this);
			mController.setAnchorView(mVideoView);
			mController.setLockScreenListener(this);
			mController.setOnShownListener(onShowMediaController);
			mController.setFileName(mMovieSelected.getTitle());
			if (mDevice.getOrientation() == Configuration.ORIENTATION_LANDSCAPE) {
				// show name video at top layout video controller
				mController.mShowExtraControl = true;
			} else {
				mController.mShowExtraControl = false;
			}
			mVideoView.setMediaController(mController);
		} else {
			mControllerBase = new MyMediaControllerBase(mVideoView, this);
		}
	}

	protected void initGestureView() {
		// mGestureView = (GestureOverlayView) findViewById(R.id.gestures_view);
		// mGestureView.addOnGesturePerformedListener(this);
		// mGestureView.setVisibility(isControllerButton ? View.GONE
		// : View.VISIBLE);
	}

	private void createSubtitleUI() {
		mSubtitleModel = MyPreferenceManager.getInstance(mContext)
				.getSubtitleSetting();
		mSubtitleText.setTextSize(mSubtitleModel.getSize());
		mSubtitleText.setTextColor(mSubtitleModel.getColor());
		mSubtitleText.setTypeface(null, mSubtitleModel.isBold() ? Typeface.BOLD
				: Typeface.NORMAL);
	}

	/**
	 * Request subtitles of film
	 */
	private void requestSubtitles() {
		String linkSub = mMovieSelected.getSubtitleLink(mEpCurrent);
		Log.d("subtitles = " + linkSub);
		VolleyClient.downloadSub(linkSub, new VolleyListener(this) {

			@Override
			public void onSuccess(String response) {
				Log.d("===================== subtitles ===================");
				Log.d(response);
				Log.d("===================================================");
			}

			@Override
			public void onStart() {
				Log.d("start request subtitles");
			}

			@Override
			public void onFinish() {
				Log.d("finish request subtitles ");
			}

			@Override
			public void onError(VolleyError error) {
				Log.e("error request subtitles" + error.getMessage());
			}
		});
	}

	/**
	 * Request root token to add random user
	 */
	private void requestToken() {

		String rootToken = mPref.getRootToken();
		if (!TextUtils.isEmpty(rootToken)) {
			// root token exist
			requestNewUser(rootToken);
			return;
		}

		// clear old root token
		mPref.saveRootToken("");

		// request root token
		VolleyClient.requestRootToken(new VolleyListener(this) {

			@Override
			public void onSuccess(String response) {
				String token = Parse.parseToken(response);
				if (!TextUtils.isEmpty(token)) {
					Log.d("request root token success " + token);
					// add random user
					requestNewUser(token);
				} else {
					Log.e("request root token failed");
					showErrorGetVideoLink();
				}
			}

			@Override
			public void onStart() {
				Log.d("start request root token");
			}

			@Override
			public void onFinish() {
				Log.d("finish request root token");
			}

			@Override
			public void onError(VolleyError error) {
				Log.e("error request root token " + error.toString());
				showErrorGetVideoLink();
			}
		});
	}

	private void requestUserToken(String userName, String pwd) {
		if (!TextUtils.isEmpty(userName) && !TextUtils.isEmpty(pwd)) {
			// request token of random user
			VolleyClient.requestToken(userName, pwd, new VolleyListener(this) {

				@Override
				public void onSuccess(String response) {
					mToken = Parse.parseToken(response);
					if (TextUtils.isEmpty(mToken)) {
						showErrorGetVideoLink();
					}
					String rawLink = mMovieSelected.getLinkMovie(mEpCurrent);
					mLinkWithToken = rawLink + "?token=" + mToken;
					initVideoView();
				}

				@Override
				public void onStart() {
					Log.d("start request random token");
					showLoading(true);
				}

				@Override
				public void onFinish() {
					Log.d("finish request random token");
					showLoading(false);
				}

				@Override
				public void onError(VolleyError error) {
					Log.e(error.toString());
					showErrorGetVideoLink();
				}
			});
		} else {
			// add new user
		}

	}

	/**
	 * Add random user by root user
	 * 
	 * @param rootToken
	 */
	private void requestNewUser(String rootToken) {
		// make random user
		mUserName = StringUtil.generateUniqueString();
		final String pwd = StringUtil.generateUniqueString();
		VolleyClient.requestNewUser(rootToken, mUserName, pwd,
				new VolleyListener(VideoPlayerActivity.this) {

					@Override
					public void onSuccess(String response) {
						if (response.equals("1")) {
							Log.d("add user success " + mUserName + ", " + pwd);
							requestUserToken(mUserName, pwd);
						} else {
							Log.e("add user failed");
							showErrorGetVideoLink();
						}
					}

					@Override
					public void onStart() {
						Log.d("start request new user");
					}

					@Override
					public void onFinish() {
						Log.d("finish request new user");
					}

					@Override
					public void onError(VolleyError error) {
						Log.e("error add user " + error.toString());
						showErrorGetVideoLink();
					}
				});
	}

	private void initVideoView() {

		if (isOpenVideoFromApp) {
			final String uri = mLinkWithToken;
			Log.d("open video with " + uri);
			if (TextUtils.isEmpty(uri)) {
				Toast.makeText(mContext, "no video link", Toast.LENGTH_LONG)
						.show();
				this.finish();
			}
			mVideoView.setVideoURI(Uri.parse(uri));
			// mVideoView.setVideoURI(Uri.parse(LINK_TEST));
		} else {
			String videoPath = mFileUri.getPath();
			File fileVideo = new File(videoPath);

			if (fileVideo.isFile()) {
				mVideoView.setVideoPath(videoPath);
			} else {
				Toast.makeText(mContext,
						"File " + mFileUri.toString() + " isn't exist",
						Toast.LENGTH_SHORT).show();
				VideoPlayerActivity.this.finish();
			}
		}

		mVideoView.setKeepScreenOn(true);
		mProgressBar = (ProgressBar) findViewById(R.id.progress_bar);
		mProgressBar.setVisibility(View.VISIBLE);
		isSubLoaded = mSubtitleController.loadSubtitles(pathSubtiles);
		mVideoView.setOnPreparedListener(mOnPreparedListener);
		mVideoView.setOnErrorListener(mOnErrorListener);
		mVideoView.setOnCompletionListener(mOnCompletionListener);
	}

	/**
	 * Save time current of video when change orientation
	 */
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		Log.d("video_time " + mVideoTimeCurrent);
		outState.putLong("video_time", mVideoTimeCurrent);
	}

	/**
	 * Get time current of video when change orientation
	 */
	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		mVideoTimeCurrent = savedInstanceState.getLong("video_time");
		Log.d("video_time " + mVideoTimeCurrent);
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		setSizeVideoViaOrientation(newConfig.orientation);
	}

	/**
	 * Change size of video when change orientation device
	 * 
	 * @param orientation
	 *            portrait or landscape
	 */
	protected void setSizeVideoViaOrientation(int orientation) {

		// Checks the orientation of the screen
		// and change size of video view
		int height = 0;
		int width = 0;
		mOrientation = orientation;
		if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
			mScrollView.setIsScrollable(false);

			// at landscape, width > height
			height = (mDisplayWidth < mDisplayHeight) ? mDisplayWidth
					: mDisplayHeight;
			width = (mDisplayWidth < mDisplayHeight) ? mDisplayHeight
					: mDisplayWidth;

			// set screen to full screen
			getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
					WindowManager.LayoutParams.FLAG_FULLSCREEN);

			// set current scale for video view
			if (mController != null) {
				mVideoView.onScale(mController.getCurrentScale());
			} else {
				mVideoView.onScale(MyMediaController.DEFAUL_SCALE);
			}

			mVideoLayout.setLayoutParams(mFullParams);
			findViewById(R.id.view_menu_search).setVisibility(View.GONE);
			findViewById(R.id.title_layout).setVisibility(View.GONE);
			findViewById(R.id.share_button).setVisibility(View.GONE);

		} else if (orientation == Configuration.ORIENTATION_PORTRAIT) {
			mScrollView.setIsScrollable(true);

			// at portrait, width < height
			width = (mDisplayWidth < mDisplayHeight) ? mDisplayWidth
					: mDisplayHeight;
			height = (int) ConvertUtil.convertDpToPixel(mContext, 200);

			getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
			Log.d("video portrait size=" + width + ", " + height);
			// mVideoView.setSize(width, height);
			mVideoView.onScale(MyVideoView.VIDEO_LAYOUT_SCALE);

			mVideoLayout.setLayoutParams(mWrapParams);
			findViewById(R.id.view_menu_search).setVisibility(View.VISIBLE);
			findViewById(R.id.title_layout).setVisibility(View.VISIBLE);
			findViewById(R.id.share_button).setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void onResume() {
		/* check setting */
		createSubtitleUI();

		isControllerButton = isControllerButton();
		initController();
		mSubtitleController.resumeSynchSubtitle();
		super.onResume();
	}

	@Override
	public void onPause() {
		super.onPause();

		// save state setting current
		if (isControllerButton) {
			MyPreferenceManager.getInstance(mContext).setVolume(
					mController.mVolumeLevelCurrent);
		} else {
			MyPreferenceManager.getInstance(mContext).setVolume(
					mControllerBase.mVolumeLevelCurrent);
		}
		mSubtitleController.pauseSynchSubtitle();
		Log.d("pause video");
		pauseVideo();

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		/* clear all data here */
		mSubtitleController.stopSynchSubtitle();
		Log.d("stop video");
		stopVideo();

		// delete user token
		if (!TextUtils.isEmpty(mToken)) {
			VolleyClient.deleteUser(mToken, mUserName,
					new VolleyListener(this) {

						@Override
						public void onSuccess(String response) {
							if (response.equals("1")) {
								Log.d("delete user success");
							} else {
								Log.e("delete user faied");
							}
						}

						@Override
						public void onStart() {
							Log.d("start request delete user");
						}

						@Override
						public void onFinish() {
							Log.d("finish request delete user");
						}

						@Override
						public void onError(VolleyError error) {
							Log.e("error delete user " + error.toString());
						}
					});
		}

	}

	/**
	 * Start video at time specific
	 * 
	 * @param currentTime
	 *            : time specific
	 */
	public void startVideo(long currentTime) {
		mProgressBar.setVisibility(View.VISIBLE);

		// load subtitle
		isSubLoaded = mSubtitleController.loadSubtitles(pathSubtiles);

		Toast.makeText(mContext,
				(isSubLoaded) ? "load subtitle success" : "load subtitle fail",

				Toast.LENGTH_SHORT).show();
		try {
			mVideoView.requestFocus();
			if (currentTime > 0) {
				mVideoView.seekTo(currentTime);
			}
			mVideoView.start();

		} catch (Exception e) {
			Log.e(e.toString());
		}
	}

	/**
	 * Menu option click
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case MENU_SETTING:
			LaunchIntent.start(this, VideoSettingActivity.class);
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * Setting for video
	 */
	OnClickListener mSettingListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			LaunchIntent.start(VideoPlayerActivity.this,
					VideoSettingActivity.class);
		}
	};

	public void pauseVideo() {
		try {
			mVideoTimeCurrent = mVideoView.getCurrentPosition();
			mVideoView.pause();
		} catch (Exception e) {
			Log.e(e.toString());
		}
	}

	public void stopVideo() {
		try {
			mVideoTimeCurrent = mVideoView.getCurrentPosition();
			mVideoView.stopPlayback();
			mVideoView.clearFocus();
		} catch (Exception e) {
			Log.e(e.toString());
		}
	}

	public boolean isPlaying() {
		return mVideoView.isPlaying();
	}

	/**
	 * Init video library vitamio
	 */
	public boolean initVitamio() {
		// check lib vitamio created
		if (!LibsChecker.checkVitamioLibs(this)) {
			Log.e("Load vitamio fail!");
			return false;
		}
		return true;
	}

	/**
	 * Set brightness
	 */
	private void setBrightness(int brightness) {

		if (mApplication.mWindowBrightness == null) {
			return;
		}

		if (brightness < 0) {
			mAppBrightness = 0;
		} else if (brightness > 255) {
			mAppBrightness = 255;
		} else {
			mAppBrightness = brightness;
		}

		BrightnessUtil.setActivityBrightness(mApplication.mWindowBrightness,
				mAppBrightness);
	}

	MediaPlayer.OnCompletionListener mOnCompletionListener = new MediaPlayer.OnCompletionListener() {

		@Override
		public void onCompletion(MediaPlayer mediaPlayer) {
			Toast.makeText(mContext, "End of Video", Toast.LENGTH_LONG).show();
		}
	};

	MediaPlayer.OnPreparedListener mOnPreparedListener = new MediaPlayer.OnPreparedListener() {

		@Override
		public void onPrepared(MediaPlayer mediaPlayer) {
			Log.d("video prepared");
			if (mVideoView.canSeekForward()) {
				if (isSubLoaded) {
					mSubtitleController.startSynchSubtitle();
				}
				mediaPlayer.setPlaybackSpeed(1.0f);
				mProgressBar.setVisibility(View.GONE);
			}
		}
	};

	protected MediaPlayer.OnErrorListener mOnErrorListener = new MediaPlayer.OnErrorListener() {

		@Override
		public boolean onError(MediaPlayer mediaPlayer, int arg1, int arg2) {
			Toast.makeText(VideoPlayerActivity.this,
					"Can not play the video!!!", Toast.LENGTH_LONG).show();
			return true;
		}
	};

	@Override
	public void onClick(View v) {

	}

	@Override
	public void lockScreen() {
		if (isOpenVideoFromApp) {
			mControllerIsLocked = true;
		}
	}

	@Override
	public void unlockScreen() {
		if (isOpenVideoFromApp) {
			mControllerIsLocked = false;
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
		}
	}

	/* Gesture play control */
	@Override
	protected void play() {
		mControllerBase.play();
	}

	@Override
	protected void pause() {
		mControllerBase.pause();
	}

	@Override
	protected void plusVolume() {
		mControllerBase.plusVolume();
	}

	@Override
	protected void minusVolume() {
		mControllerBase.minusVolume();
	}

	@Override
	protected void capture() {
		mControllerBase.capture();
	}

	@Override
	protected void mute() {
		mControllerBase.muteVolume();
	}

	@Override
	protected void scale() {
		mControllerBase.scale();
	}

	@Override
	public void onUpdateSubtitle(final String subtitle) {
		// TODO Auto-generated method stub
		if (subtitle != null) {
			mSubtitleText.post(new Runnable() {
				@Override
				public void run() {
					mSubtitleText.setText(Html.fromHtml(subtitle));
				}
			});
		}
	}

	@Override
	public void setTagMenu() {
		if (mHomeId.equals(AppConstants.ID_IPHIM)) {
			mTagMenu = AppConstants.MENU_IPHIM;
		} else {
			mTagMenu = AppConstants.MENU_ICLIP;
		}
	}

	private void showErrorGetVideoLink() {
		MyToast.show(this, "Không thể lấy video link");
		return;
	}

	/**
	 * Show name file when video at landscape
	 */
	private OnShowListener onShowMediaController = new OnShowListener() {

		@Override
		public void onShow() {
			if (mOrientation == Configuration.ORIENTATION_LANDSCAPE) {
				if (!mControllerIsLocked) {
					mController.showNameFilmLayout(true);
					mController.showScaleBtn(true);
					mController.showLockBtn(true);
				}
			} else {
				mController.showNameFilmLayout(false);
				mController.showScaleBtn(false);
				mController.showLockBtn(false);
			}
		}

		@Override
		public void onHide() {
			// nothing
		}

	};

}
